<?php

class Signup extends CI_Controller {

	public function __construct() {
		parent::__construct();

		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');
		$this->load->model('user');
	}
	
	public function _remap($method, $params)
	{
		$u = Current_User::user();
		if(!$u) {
			$this->load->view('pages/user/logout', array('info' => '请先登录系统'));
		}else{
			$role = Current_User::my_role();
			if($role == Current_User::$administrtor) {
				// $this->$method();
				if (method_exists($this, $method))
			    {
			        return call_user_func_array(array($this, $method), $params);
			    }
			}
			else redirect("/home/nopower");
		}
	}
	
	public function index() {
		$ret = $this->user->get_roles();
		$data = array();
		foreach ($ret as $role){
			$data[$role->id] = $role->name;
		}
		$this->load->view('pages/user/signup_form', array('roles' => $data));
	}
	
	public function edit($id) {
		$ret = $this->user->get_roles();
		$roles = array();
		foreach ($ret as $role){
			$roles[$role->id] = $role->name;
		}
		$data = array(
			'roles' => $roles
		);
		$this->load->view('pages/user/edit_form', $data);
	}

	public function submit() {

		if ($this->_submit_validate() === FALSE) {
			$this->index();
			return;
		}

		$data = array(
            'account' => $this->input->post('account') ,
            'name' => $this->input->post('name') ,
            'password' => md5($this->input->post('password')),
			'sex' => $this->input->post('sex'),
			'role_id' => $this->input->post('role_id'),
			'email' => $this->input->post('email'),
			'phone' => $this->input->post('phone'),
			'insert_time' => date('Y-m-d H:i:s',time())
        );

		$this->db->insert('user', $data);

		/*$user = new Entities\User;
	    $user->setAccount($this->input->post('account'));
	    $user->setName($this->input->post('name'));
	    $user->setPassword(md5($this->input->post('password')));
	    $user->setRoleId($this->input->post('role_id'));
	    $user->setEmail($this->input->post('email'));
	    $user->setSex($this->input->post('sex'));
	    $user->setPhone($this->input->post('phone'));
	    $user->setInsertTime(new DateTime());
	    
	    $this->doctrine->em->persist($user);
		$this->doctrine->em->flush();*/
		
		redirect("/user/user_list");

	}
	
	public function modify($user_id) {

		if ($this->_submit_validate() === FALSE) {
			$this->index();
			return;
		}

		$data = array(
            'account' => $this->input->post('account') ,
            'name' => $this->input->post('name') ,
            'password' => md5($this->input->post('password')),
			'sex' => $this->input->post('sex'),
			'role_id' => $this->input->post('role_id'),
			'email' => $this->input->post('email'),
			'phone' => $this->input->post('phone')
        );

		$this->db->update('user', $data, array('user_id' => $user_id));
		
		/*$user = $this->doctrine->em->find('Entities\User', $user_id);
		
	    $user->setAccount($this->input->post('account'));
	    $user->setName($this->input->post('name'));
	    $user->setPassword(md5($this->input->post('password')));
	    $user->setRoleId($this->input->post('role_id'));
	    $user->setEmail($this->input->post('email'));
	    $user->setSex($this->input->post('sex'));
	    $user->setPhone($this->input->post('phone'));
	    //$user->setInsertTime(new DateTime());
	    
	    $this->doctrine->em->persist($user);
		$this->doctrine->em->flush();*/
		
		redirect("/user/user_list");

	}

	private function _submit_validate() {

		// validation rules
		$this->form_validation->set_rules('account', 'Account',
			'required|alpha_numeric|min_length[4]|max_length[12]');
		$this->form_validation->set_rules('name', 'Name',
			'trim|required|min_length[4]|max_length[12]');
		$this->form_validation->set_rules('password', 'Password',
			'required|min_length[6]|max_length[12]');

		/*$this->form_validation->set_rules('passconf', 'Confirm Password',
			'required|matches[password]');*/

		$this->form_validation->set_rules('email', 'E-mail',
			'required|valid_email');

		return $this->form_validation->run();

	}
}
